import random
import os
import re
import time

admin=['天一','大空翼','暖哥','jski匪姐','柚子','大菠','赵嘻嘻','陈6岁🏂']
keys=['【时间】','【目的】','【费用】','【人数】','【联系】']

def clearmsg():
    f = open('fyueche.txt','w+')
    f.write('')
    f.close()

def addmsg(s,name,qunnum):
    file = open("fyueche.txt",'r')
    lines = file.readlines()
    file.close()
    name=name+' (约车%s群)'%(qunnum)
    f = open('fyueche.txt','a')
    if lines==[]:
        f.write(str(1)+':'+s+'@%s '%(name)+'\n')
    else:
        num=int((lines[-1])[0:1])
        f.write(str(num+1)+':'+s+'@%s '%(name)+'\n')
    f.close()

def showmsg():
    f=open('fyueche.txt', 'r')
    lines = f.readlines()
    strshow = '📣信息发布助手\n发布方法(j发+正确格式内容)\n删除方法(j删+序号)\n查看方法(直接发个j)\n查看格式(直接发个j发)\n'
    for line in lines:
        line = line.replace('【','\n【')
        strshow = strshow+'\n'+line
    f.close()
    return strshow

def delnum(delnum,name):
    file = open("fyueche.txt",'r')
    lines = file.readlines()
    n = 1
    with open("fyueche.txt",'w') as f:
        for line in lines:
            num = line[0:1]
            if int(n) == int(delnum):
                if name+' ' in line or name in admin:
                    msg = '拼车信息 %s 已删除'%(delnum)
                    delnum = False

                else:
                    newline=str(n)+line[1:]
                    f.write(newline)
                    n=n+1
                    msg = '只有发布人和管理员可以删除'
            else:
                newline = str(n)+line[1:]
                f.write(newline) 
                n=n+1
        f.close()
        return msg

def msghelp(act,name,qunnum):
    act = act.replace('\n', '')
    act = act.replace(' ','')
    act = act.replace('***复制本条发布拼车信息***','')
    name=name.replace(' ','')
    qunnum = int(qunnum)
    for key in keys:
        flag = key in act
        flag = flag and flag
    if act == 'j':
        msgshow = showmsg()
        return msgshow
    elif 'j删' in act:
        act = act.replace('j删','')
        act=int(act)
        return delnum(act,name)
    elif 'j发' in act:
        if flag:
            act = act.replace('j发','') 
            addmsg(act,name,qunnum)
            msgshow = showmsg()
            return msgshow
        else:
            msg = 'j发\n【人找车/车找人】\n【时间】\n【出发】\n【目的】\n【人数】\n【费用】\n【联系】\n***复制本条发布拼车信息***'
            return msg
